<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2048</title>
</head>

<body>
    <div id="app">
        <button @click="createNumBegin">开始游戏</button>
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            // 随机数
            num: 0,
            score: 0,
            // 储存随机生成的数的二维存放数组
            arr: [
                [0, 0, 0, 0],
                [0, 0, 0, 0],
                [0, 0, 0, 0],
                [0, 0, 0, 0]
            ],
            bottom: 0
        },
        methods: {
            // 随机生成2 或 4
            random: function () {
                // 生成死循环
                for (; ;) {
                    // 获得二维数组的随机位置
                    var r = Math.floor(Math.random() * 4);
                    var c = Math.floor(Math.random() * 4);
                    // 判断生成的随机二维数组位置上的数字为0才随机生成2或4
                    if (this.arr[r][c] == 0) {
                        var num = Math.random() > 0.5 ? 2 : 4;
                        this.arr[r][c] = num;
                        break;
                    }
                }
            },
            // 展示数组模板
            showArr: function () {
                let arrShow =
                    `
                    ${this.arr[0][0]} ${this.arr[0][1]} ${this.arr[0][2]} ${this.arr[0][3]}
                    ${this.arr[1][0]} ${this.arr[1][1]} ${this.arr[1][2]} ${this.arr[1][3]}
                    ${this.arr[2][0]} ${this.arr[2][1]} ${this.arr[2][2]} ${this.arr[2][3]}
                    ${this.arr[3][0]} ${this.arr[3][1]} ${this.arr[3][2]} ${this.arr[3][3]}
                    `
                console.log(arrShow);
            },
            createNumBegin: function () {
                for (let a = 0; a < this.arr.length; a++) {
                    for (let b = 0; b < this.arr.length; b++) {
                        this.bottom += this.arr[a][b]
                    }
                }
                console.log(this.bottom);
                if (this.bottom == 0) {
                    this.random();
                    this.random();
                    this.showArr();
                } else {
                    this.random();
                    this.showArr();
                }
            },
            moveUp: function () {
                console.log('向上移动了');
                // 先逆时针旋转，再顺时针旋转  
                this.arrShow =
                    `
                            ${this.arr[0][3]} ${this.arr[1][3]} ${this.arr[2][3]} ${this.arr[3][3]}
                            ${this.arr[0][2]} ${this.arr[1][2]} ${this.arr[2][2]} ${this.arr[3][2]}
                            ${this.arr[0][1]} ${this.arr[1][1]} ${this.arr[2][1]} ${this.arr[3][1]}
                            ${this.arr[0][0]} ${this.arr[1][0]} ${this.arr[2][0]} ${this.arr[3][0]}
                            `
                            this.arrShow
                for (let i = 0; i < 100; i++) {
                    const leftBtm = this.arr[0][i]
                    if (leftBtm != 0) {
                        if (leftBtm == this.arr[0][0]) {
                            leftBtm *= 2;
                            this.score = this.score + leftBtm
                            console.log(this.score);
                        } else if (leftBtm == this.arr[0][1]) {
                            leftBtm *= 2;
                            this.score = this.score + leftBtm
                            console.log(this.score);
                        } else if (leftBtm == this.arr[0][2]) {
                            leftBtm *= 2;
                            this.score = this.score + leftBtm
                            console.log(this.score);
                        } else if (leftBtm == this.arr[0][3]) {
                            leftBtm *= 2;
                            this.score = this.score + leftBtm
                            console.log(this.score);
                        }
                    }
                }
                // this.arrShow =
                //     `
                //     ${this.arr[0][0]} ${this.arr[0][1]} ${this.arr[0][2]} ${this.arr[0][3]}
                //     ${this.arr[1][0]} ${this.arr[1][1]} ${this.arr[1][2]} ${this.arr[1][3]}
                //     ${this.arr[2][0]} ${this.arr[2][1]} ${this.arr[2][2]} ${this.arr[2][3]}
                //     ${this.arr[3][0]} ${this.arr[3][1]} ${this.arr[3][2]} ${this.arr[3][3]}
                //             `
                this.random();
                console.log(this.arrShow);
            },
            moveDown: function () {
                console.log('向下移动了');
            },
            moveLift: function () {
                console.log('向左移动了');
            },
            moveRight: function () {
                console.log('向右移动了');
            },
            // 定义监听键盘事件
            keyFun: function (event) {
                const e = event || window.event || arguments.callee.caller.arguments[0];
                // console.log(e.key);
                // console.log(e.keyCode);
                switch (e.keyCode) {
                    case 38:// 上移
                        this.moveUp();
                        break;
                    case 40:// 下移
                        this.moveDown();
                        break;
                    case 37:// 左移
                        this.moveLift();
                        break;
                    case 39:// 右移
                        this.moveRight();
                        break;
                }
            }
        },
        mounted() {
            // 监听键盘
            window.addEventListener("keydown", this.keyFun);
        },
        computed: {
            showNumBegin: function () {
                return this.createNumBegin();
            },
        }
    })
</script>

</html>